package com.chen.redis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 *内存数据库redis测试类
 *@author：email: <a href="cyb@ewppay.com"> Administrator </a> 
 *@Date 2017-2-24 下午5:51:37 
 */

public class Test {
	/**
	 * 
	 * @Description: 得到连接池
	 * @param @return   
	 * @return JedisPool  
	 * @throws
	 * @author cyb
	 * @date 2017-2-24
	 */
	public static JedisPool getPool(){
		JedisPoolConfig poolConfig = new JedisPoolConfig();
		//最大链接数 
		poolConfig.setMaxTotal(5);
		//最大空闲链接数
		poolConfig.setMaxIdle(5);
		//最小空闲链接数
		poolConfig.setMinIdle(1);
		//链接数耗尽是否阻塞
		poolConfig.setBlockWhenExhausted(true);
		//逐出连接的最小空闲时间 默认1800000毫秒(30分钟)
		poolConfig.setMinEvictableIdleTimeMillis(1800000);
		//逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1
		poolConfig.setTimeBetweenEvictionRunsMillis(30000);
		//每次逐出检查时 逐出的最大数目 如果为负数就是 : 1/abs(n), 默认3
		poolConfig.setNumTestsPerEvictionRun(3);
		//在空闲时检查有效性, 默认false 
		poolConfig.setTestWhileIdle(true);
		
		JedisPool pool = new JedisPool(poolConfig,"192.168.20.230",6379,2000,"123456",15);
		return pool;
	}
	
	/**
	 * 
	 * @Description: 连接返还连接池
	 * @param @param pool
	 * @param @param jedis   
	 * @return void  
	 * @throws
	 * @author cyb
	 * @date 2017-2-24
	 */
	public static void returnResource(JedisPool pool,Jedis jedis){
		if(jedis != null){
			pool.returnResource(jedis);
		}
	}
	
	public static void sremInSet(String key,String... values){
		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			jedis.srem(key, values);
		} catch (Exception e) {
			e.printStackTrace();
			pool.returnBrokenResource(jedis);
		} finally{
			returnResource(pool,jedis);
		}
	}
	
	public static String getKey(String key){
		String value = "";
		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			value = jedis.get(key);
		} catch (Exception e) {
			e.printStackTrace();
			pool.returnBrokenResource(jedis);
		} finally{
			returnResource(pool,jedis);
		}
		return value;
	}

	public static void main(String[] args) {
		//System.out.println(getKey("ip:realnameauth:192.168.20.23"));
		sremInSet("test","2","3");
	}

}
